//
//  shipListViewController.m
//  WHB_cargo
//
//  Created by 徐国庆 on 15/12/9.
//  Copyright (c) 2015年 turing. All rights reserved.
//

#import "shipListViewController.h"
#import "shipListCell.h"
#import "goingCell.h"
#import "historyCell.h"
#import "orderDetailViewController.h"
#import "vechileDetailViewController.h"

@interface shipListViewController ()<UITableViewDataSource, UITableViewDelegate>

@property(nonatomic, strong)UIButton    *comparePriceButton; // 比价中
@property(nonatomic, strong)UIButton    *goingButton;        // 进行中
@property(nonatomic, strong)UIButton    *historyButton;      // 历史
@property(nonatomic, strong)UILabel     *removeLineLabel;    // 随着按钮点击位置变化的移动的label
@property(nonatomic, strong)UITableView *tableView;          // 列表
@property(nonatomic, strong)NSString    *remark;             // 标记点击的按钮

@end

@implementation shipListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navtitle.text = @"货单";
    self.leftButton.hidden = YES;
    [self initWithView];
    self.remark = @"0";
}

#pragma mark UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 50;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([self.remark isEqualToString:@"0"]) {
        NSString * indentifer = @"shipListCell";
        
        shipListCell *cell = (shipListCell *)[tableView dequeueReusableCellWithIdentifier:indentifer];
        if (!cell)
        {
            cell=[[NSBundle mainBundle] loadNibNamed:@"shipListCell" owner:self options:nil][0];
        }
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.setOutAddressLabel.text = @"新疆维吾尔自治区";// 出发地
        cell.aimAddressLabel.text = @"新疆维吾尔自治区";// 目的地
        cell.separatorLineLabel.backgroundColor = [UIColor HexStrWithColor:@"d0d0d0"];// “出发地”“目的地”与“取消订单”中间的分隔线
        cell.cancelOrderButton.backgroundColor = [UIColor HexStrWithColor:@"EA0000"];// “取消订单”按钮
        cell.aimAddressBottomLineLabel.backgroundColor = [UIColor HexStrWithColor:@"d0d0d0"];// “目的地”下的“线”
        cell.businessPriceLabel.text = @"企业报价:";// 企业报价
        cell.w_businessPriceLabel.text = @"100000元";// "企业报的价"
        cell.loadingTimeLabel.text = @"装货时间:";// “装货时间”
        cell.w_loadingTimeLabel.text = @"2015年11月11日";// 装货的时间
        cell.listRequireLabel.text = @"运单要求:";// 运单要求
        cell.w_listRequireLabel.text = @"轻拿轻放";// 运单的具体要求
        cell.listRequireBottomLineLabel.backgroundColor = [UIColor HexStrWithColor:@"d0d0d0"];// “运单要求”下面的“线”
        cell.mediumLabel.text = @"运输介质:";//介质
        cell.w_mediumLabel.text = @"汽油";
        cell.weightLabel.text = @"重量:";
        cell.w_weightLabel.text = @"12吨";
        cell.weightBottomLineLabel.backgroundColor = [UIColor HexStrWithColor:@"d0d0d0"];
        cell.vehicle.text = @"推送:";
        cell.w_vehicleLabel.text = @"京A12345";
        cell.w_vehicleLabel.textColor = [UIColor HexStrWithColor:@"AE0000"];
        cell.checkVehicleButton.backgroundColor = [UIColor HexStrWithColor:@"0000C6"];
        cell.checkVehicleButton.tag = indexPath.row;
        [cell.checkVehicleButton addTarget:self action:@selector(checkVechileButtonAction:) forControlEvents:UIControlEventTouchDown];
        cell.vehicleBottomLineLabel.backgroundColor = [UIColor HexStrWithColor:@"d0d0d0"];
        return cell;
    }else if ([self.remark isEqualToString:@"1"]){
        
        NSString * indentifer = @"goingCell";
        
        goingCell *cell = (goingCell *)[tableView dequeueReusableCellWithIdentifier:indentifer];
        if (!cell)
        {
            cell=[[NSBundle mainBundle] loadNibNamed:@"goingCell" owner:self options:nil][0];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell.orderListNumberView addSubview:[self titleAndContent:@"订单号:" andContent:@"201511110001" withToAddress:nil]];
        [cell.addressView addSubview:[self titleAndContent:@"位置:" andContent:@"新疆维吾尔自治区" withToAddress:@"新疆维吾尔自治区"]];
        [cell.mediumView addSubview:[self titleAndContent:@"介质:" andContent:@"汽油" withToAddress:nil]];
        [cell.weightView addSubview:[self titleAndContent:@"重量:" andContent:@"10吨" withToAddress:nil]];
        [cell.priceView addSubview:[self titleAndContent:@"成交价:" andContent:@"100000元" withToAddress:nil]];
        [cell.forwardingView addSubview:[self titleAndContent:@"承运商:" andContent:@"京A12345" withToAddress:nil]];
        [cell.orderStateView addSubview:[self titleAndContent:@"货单状态:" andContent:@"取货中" withToAddress:nil]];
        cell.realTimeAddressButton.backgroundColor = [UIColor HexStrWithColor:@"FF8040"];
        cell.receiveShipOverButton.backgroundColor = [UIColor HexStrWithColor:@"A23400"];
        cell.lastBottomLineLabel.layer.borderColor = [UIColor HexStrWithColor:@"d0d0d0"].CGColor;
        cell.lastBottomLineLabel.backgroundColor = [UIColor HexStrWithColor:@"FCFCFC"];
        return cell;
    } else if ([self.remark isEqualToString:@"2"]){
        NSString * indentifer = @"historyShipCell";
        
        historyCell *cell = (historyCell *)[tableView dequeueReusableCellWithIdentifier:indentifer];
        if (!cell)
        {
            cell=[[NSBundle mainBundle] loadNibNamed:@"historyShipCell" owner:self options:nil][0];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell.setoutDataView addSubview:[self titleAndContent:@"发货日期:" andContent:@"2015年11月11日" withToAddress:nil]];
        [cell.addressView addSubview:[self titleAndContent:@"位置:" andContent:@"新疆维吾尔自治区" withToAddress:@"新疆维吾尔自治区"]];
        [cell.mediumView addSubview:[self titleAndContent:@"介质:" andContent:@"汽油" withToAddress:nil]];
        [cell.weightView addSubview:[self titleAndContent:@"重量:" andContent:@"10吨" withToAddress:nil]];
        [cell.orderStateView addSubview:[self titleAndContent:@"货单状态:" andContent:@"已签收" withToAddress:nil]];
        cell.lastBottomLabel.backgroundColor = [UIColor HexStrWithColor:@"FCFCFC"];
        cell.lastBottomLabel.layer.borderColor = [UIColor HexStrWithColor:@"d0d0d0"].CGColor;
        
        return cell;
    }
    static NSString *indentifer = @"goingCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:indentifer];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indentifer];
    }

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([self.remark isEqualToString:@"2"]) {
        orderDetailViewController *orderDetailVC = [[orderDetailViewController alloc] init];
        [self.navigationController pushViewController:orderDetailVC animated:YES];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([self.remark isEqualToString:@"0"]) {
        return 310;
    } else if ([self.remark isEqualToString:@"1"]){
        return 330;
    } else if ([self.remark isEqualToString:@"2"]){
        return 230;
    }
    return 410;
}

#pragma mark "比价中"action
- (void)comparePriceButtonAction:(UIButton *)button{
    
    [self clickButtonAction:self.comparePriceButton darkButtonFirst:self.goingButton darkButtonSecond:self.historyButton changeRemoveLabelPostion:0];
    self.remark = @"0";
    [self.tableView reloadData];
}

#pragma mark “进行中”action
- (void)goingButtonAction:(UIButton *)button{
    
    [self clickButtonAction:self.goingButton darkButtonFirst:self.comparePriceButton darkButtonSecond:self.historyButton changeRemoveLabelPostion:kUIScreenWidth / 3];
    self.remark = @"1";
    [self.tableView reloadData];
}

#pragma mark "历史"action
- (void)historyButtonAction:(UIButton *)button{
    
    [self clickButtonAction:self.historyButton darkButtonFirst:self.comparePriceButton darkButtonSecond:self.goingButton changeRemoveLabelPostion:kUIScreenWidth / 3 * 2];
    self.remark = @"2";
    [self.tableView reloadData];
}

#pragma mark “查看推送车辆”
- (void)checkVechileButtonAction:(UIButton *)button{
    vechileDetailViewController *vechileDetailVC = [[vechileDetailViewController alloc] init];
    [self.navigationController pushViewController:vechileDetailVC animated:YES];
}

/**
 *  点击“比价中”“进行中”“历史”三个按钮之间颜色以及移动条的变化
 *
 *  @param hightLightButton 被选中点亮的按钮
 *  @param darkButtonFirst  第一个灰色按钮
 *  @param darkButtonSecond 第二个灰色按钮
 *  @param postion          移动条移动的位置
 */
- (void)clickButtonAction:(UIButton *)hightLightButton darkButtonFirst:(UIButton *)darkButtonFirst darkButtonSecond:(UIButton *)darkButtonSecond changeRemoveLabelPostion:(float)postion{
    
    // 点亮按钮颜色变亮 其他两个按钮颜色变灰 移动条移动到点亮按钮下面
    [hightLightButton setTitleColor:[UIColor HexStrWithColor:@"FF2D2D"] forState:UIControlStateNormal];
    [darkButtonFirst setTitleColor:[UIColor HexStrWithColor:@"8E8E8E"] forState:UIControlStateNormal];
    [darkButtonSecond setTitleColor:[UIColor HexStrWithColor:@"8E8E8E"] forState:UIControlStateNormal];

    // 移动条移动到被点亮的按钮下面,使用动画
    [UIView animateWithDuration:1 delay:0 usingSpringWithDamping:0.8 initialSpringVelocity:5 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        self.removeLineLabel.frame = CGRectMake( postion, self.historyButton.frame.origin.y + self.historyButton.frame.size.height, kUIScreenWidth / 3, 2);
    } completion:^(BOOL finished) {
    }];
}


/**
 *  初始化视图
 */
- (void)initWithView{
    // 比价上面的“线”
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, kNavHeight + kStateHeight, kUIScreenWidth, 1)];
    label.backgroundColor = [UIColor HexStrWithColor:@"d0d0d0"];
    [self.view addSubview:label];
    
    // “比价中”
    self.comparePriceButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.comparePriceButton.frame = CGRectMake(0, kNavHeight + kStateHeight, kUIScreenWidth / 3, 50);
    [self.comparePriceButton setTitle:@"比价中" forState:UIControlStateNormal];
    [self.comparePriceButton setTitleColor:[UIColor HexStrWithColor:@"FF2D2D"] forState:UIControlStateNormal];
    self.comparePriceButton.titleLabel.font = [UIFont systemFontOfSize:15];
    [self.comparePriceButton addTarget:self action:@selector(comparePriceButtonAction:) forControlEvents:UIControlEventTouchDown];
    [self.view addSubview:self.comparePriceButton];
    
    // “进行中”
    self.goingButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.goingButton.frame = CGRectMake(kUIScreenWidth / 3, kNavHeight + kStateHeight, kUIScreenWidth / 3, 50);
    [self.goingButton setTitle:@"进行中" forState:UIControlStateNormal];
    [self.goingButton setTitleColor:[UIColor HexStrWithColor:@"8E8E8E"] forState:UIControlStateNormal];
    self.goingButton.titleLabel.font = [UIFont systemFontOfSize:15];
    [self.goingButton addTarget:self action:@selector(goingButtonAction:) forControlEvents:UIControlEventTouchDown];
    [self.view addSubview:self.goingButton];

    // “历史”
    self.historyButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.historyButton.frame = CGRectMake(kUIScreenWidth / 3 * 2, kNavHeight + kStateHeight, kUIScreenWidth / 3, 50);
    [self.historyButton setTitle:@"历史" forState:UIControlStateNormal];
    [self.historyButton setTitleColor:[UIColor HexStrWithColor:@"8E8E8E"] forState:UIControlStateNormal];
    self.historyButton.titleLabel.font = [UIFont systemFontOfSize:15];
    [self.historyButton addTarget:self action:@selector(historyButtonAction:) forControlEvents:UIControlEventTouchDown];
    [self.view addSubview:self.historyButton];

    // "比价中"“进行中”“历史”下面的“线”
    UILabel *bottomLineLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, self.historyButton.frame.origin.y + self.historyButton.frame.size.height, kUIScreenWidth, 1)];
    bottomLineLabel.backgroundColor = [UIColor HexStrWithColor:@"d0d0d0"];
    [self.view addSubview:bottomLineLabel];
    
    // 随着按钮点击位置变化的红色线
    self.removeLineLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, bottomLineLabel.frame.origin.y, kUIScreenWidth / 3, 2)];
    self.removeLineLabel.backgroundColor = [UIColor HexStrWithColor:@"FF0000"];
    [self.view addSubview:self.removeLineLabel];
    
    // 初始化列表
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, self.removeLineLabel.frame.origin.y + self.removeLineLabel.frame.size.height, kUIScreenWidth, kUIScreenHeight - self.removeLineLabel.frame.origin.y - 42) style:UITableViewStylePlain];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.tableView];
    
}

/**
 *  自定义cell
 *
 *  @param title     标题
 *  @param content   内容
 *  @param toAddress 如果是地址显示到达的位置 如果该参数为nil自动设置为不显示
 *
 *  @return 生成一个标题和一个内容的view
 */
- (UIView *)titleAndContent:(NSString *)title andContent:(NSString *)content withToAddress:(NSString *)toAddress{
    UIView *bottomView = [[UIView alloc] init];
    
    // 标题
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 80, 20)];
    titleLabel.text = title;
    titleLabel.textAlignment = 2;
    titleLabel.font = [UIFont systemFontOfSize:15];
    [bottomView addSubview:titleLabel];
    
    // 内容
    UILabel *contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(titleLabel.frame.origin.x + titleLabel.frame.size.width + 20, 10, 200, 20)];
    contentLabel.text = content;
    contentLabel.textAlignment = 0;
    [contentLabel sizeToFit];
    contentLabel.font = [UIFont systemFontOfSize:15];
    // 如果标题为货单状态 改变详情颜色
    if ([title isEqualToString:@"货单状态:"]) {
        contentLabel.textColor = [UIColor HexStrWithColor:@"FF0000"];
    }
//    if ([contentLabel.text isEqualToString:@"已确认"]) {
//        contentLabel.textColor = [UIColor HexStrWithColor:@"8CEA00"];
//    } else if ([contentLabel.text isEqualToString:@"未确认"]){
//        contentLabel.textColor = [UIColor HexStrWithColor:@"FF2D2D"];
//    }
    [bottomView addSubview:contentLabel];
    
    if ([title isEqualToString:@"位置:"]) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(contentLabel.frame.origin.x + contentLabel.frame.size.width / 2, contentLabel.frame.origin.y + contentLabel.frame.size.height + 5, 18 / 2.0, 21 / 2.0)];
        imageView.image = [UIImage imageNamed:@"jt_xl"];
        imageView.transform=CGAffineTransformMakeRotation(1.55);
        [bottomView addSubview:imageView];
        
        UILabel *toAddLabel = [[UILabel alloc] initWithFrame:CGRectMake(contentLabel.frame.origin.x, imageView.frame.origin.y + imageView.frame.size.height + 5, 200, 20)];
        toAddLabel.text = toAddress;
        [bottomView addSubview:toAddLabel];
    }
    
    return bottomView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
